package com.ywh.composetest.widget

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

@Composable
fun TextFieldTestCompose(
    content: MutableState<String>,
    filters: ArrayList<TextFieldFilter>? = null,
    onValueChanged: (String) -> Unit
) {
    Box(
        modifier = Modifier
            .height(50.dp)
            .border(1.dp, Color.Red, RoundedCornerShape(8.dp))
            .padding(horizontal = 10.dp),
        contentAlignment = Alignment.Center
    ) {
//        if (content.value.isEmpty()) {
//            Text(text = "请输入内容", color = Color.Green, fontSize = 12.sp)
//        }
        BasicTextField(
            value = content.value,
            onValueChange = { value ->
                var result = value
                filters?.forEach {
                    result = it.filter(result)
                }
                content.value = result
                onValueChanged.invoke(result)
            },
            modifier = Modifier
                .fillMaxWidth(),
            decorationBox = {
                if (content.value.isEmpty()) {
                    Text(
                        text = "请输入数字",
                        fontSize = 14.sp,
                        color = Color.Green
                    )
                }
                it.invoke()
            },
            enabled = true,
            readOnly = false,
            textStyle = TextStyle(
                color = Color.Red,
                fontSize = 12.sp,
                fontWeight = FontWeight.Medium,
            ),
            keyboardActions = KeyboardActions(onDone = {
                Log.e("TextFieldTest", ":onDone")
            }, onSend = {
                Log.e("TextFieldTest", ":onSend")
            }, onSearch = {
                Log.e("TextFieldTest", ":onSearch")
            }),
            keyboardOptions = KeyboardOptions(imeAction = ImeAction.Next),
        )

    }
}

interface TextFieldFilter {
    fun filter(value: String): String
}

class TextFieldLengthFilter(private val maxLength: Int) : TextFieldFilter {
    override fun filter(value: String): String = value.take(maxLength)
}

class TextFieldNumberFilter : TextFieldFilter {
    private val allowedCharacters = Regex("[0-9]")
    override fun filter(value: String): String {
        return value.filter { allowedCharacters.matches(it.toString()) }
    }
}

@Preview
@Composable
fun PreviewTextFieldTestCompose() {
    TextFieldTestCompose(content = remember {
        mutableStateOf("科技撒可富凯撒就开始科技撒可富凯撒就开始科技撒可富凯撒就开始科技撒撒就开始科技撒可富凯撒就开始科技撒可富凯撒")
    }) {
    }
}